package com.hairline.controller;
import com.hairline.bean.Coupon;
import com.hairline.bean.User;
import com.hairline.bean.vo.BaseRespVo;
import com.hairline.bean.vo.MyCoupon;
import com.hairline.service.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    //显示全部优惠券
    @RequestMapping("/wx/coupon/list")
    public BaseRespVo couponList(Integer page,Integer size){
        Map<String,Object> map = wxCouponService.getAllCoupon(page,size);
        return BaseRespVo.ok(map);
    }

    //显示当前用户优惠劵
    @RequestMapping("/wx/coupon/mylist")
    public BaseRespVo couponMyList(Integer status, Integer page, Integer size){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        Map<String,Object> map  = wxCouponService.getMyCoupon(userId,status,page,size);
        return BaseRespVo.ok(map);
    }

    //显示可用优惠劵
    @RequestMapping("/wx/coupon/selectlist")
    public BaseRespVo couponSelect(){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        List<MyCoupon> myCoupons = wxCouponService.selectMyCoupon(userId);
        return BaseRespVo.ok(myCoupons);
    }

    //领取优惠劵

    @PostMapping("/wx/coupon/receive")
    public BaseRespVo couponReceive(@RequestBody Map couponId){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        Coupon coupon = wxCouponService.selectCoupon((Integer) couponId.get("couponId"));
        if (coupon.getTotal().intValue() < 0){
            return BaseRespVo.fail("优惠劵已经领完了",740L);
        }

        Integer userNum = wxCouponService.selectCouponUser(userId,coupon.getId());
        if (userNum.intValue() != 0 && coupon.getLimit().intValue() <= userNum){
            return BaseRespVo.fail("优惠劵已经领过了",740L);
        }

        //用户增加优惠劵,库存减少
        wxCouponService.addUserCoupon(userId,coupon);
        return BaseRespVo.ok("优惠劵领取成功");
    }

    //兑换优惠劵
    @PostMapping("/wx/coupon/exchange")
    public BaseRespVo couponExchange(@RequestBody Map code) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        //通过兑换码查找优惠劵
        Coupon coupon = wxCouponService.getCouponByCode((String) code.get("code"));
        if (coupon == null){
            return BaseRespVo.fail("兑换码错误",742L);
        }

        if (coupon.getTotal().intValue() < 0){
            return BaseRespVo.fail("优惠劵已经领完了",740L);
        }

        Integer userNum = wxCouponService.selectCouponUser(userId,coupon.getId());
        if (userNum.intValue() != 0 && coupon.getLimit().intValue() <= userNum){
            return BaseRespVo.fail("优惠劵已经兑换过了",740L);
        }

        //用户增加优惠劵,库存减少
        wxCouponService.addUserCoupon(userId,coupon);

        return BaseRespVo.ok("兑换成功");
    }

}
